package com.intelligent.bot.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.intelligent.bot.base.exception.E;
import com.intelligent.bot.base.result.B;
import com.intelligent.bot.dao.ClassifyVideoDao;
import com.intelligent.bot.dao.VideoDao;
import com.intelligent.bot.model.ClassifyVideo;
import com.intelligent.bot.model.Team;
import com.intelligent.bot.model.Video;
import com.intelligent.bot.model.base.BaseDeleteEntity;
import com.intelligent.bot.model.req.VideoAddReq;
import com.intelligent.bot.model.req.VideoGetListReq;
import com.intelligent.bot.model.req.VideoUpdateReq;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional(rollbackFor = E.class)
public class VideoServiceImpl extends ServiceImpl<VideoDao, Video> implements IVideoService {

    @Resource
    IClassifyVideoService classifyVideoService;

    @Override
    public B<Page<Video>> list(VideoGetListReq req) {
        Page<Video> page = new Page<>(req.getPageNumber(), req.getPageSize());
        return B.okBuild(baseMapper.getList(page, req));
    }

    @Override
    public B<Void> add(VideoAddReq req) {
        Long count = this.lambdaQuery().eq(Video::getVideoUrl, req.getVideoUrl()).count();
        if (count > 0) {
            throw new E("该视频已存在");
        }
        Video video = BeanUtil.copyProperties(req, Video.class);
        this.save(video);
        if(req.getClassifyId() != null){
            ClassifyVideo classifyVideo = new ClassifyVideo();
            classifyVideo.setClassifyId(req.getClassifyId());
            classifyVideo.setVideoId(video.getId());
            this.classifyVideoService.save(classifyVideo);
        }
        return B.okBuild();
    }

    @Override
    public B<Void> update(VideoUpdateReq req) {
        Long count = this.lambdaQuery().eq(Video::getVideoUrl, req.getVideoUrl()).ne(Video::getId, req.getId()).count();
        if (count > 0) {
            throw new E("该视频已存在");
        }
        Video video = BeanUtil.copyProperties(req, Video.class);
        this.saveOrUpdate(video);
        if(req.getClassifyId() != null){
            Long count1 = classifyVideoService.lambdaQuery()
                    .eq(ClassifyVideo::getClassifyId, req.getClassifyId())
                    .eq(ClassifyVideo::getVideoId, req.getId())
                    .count();
            if(count1 == 0){
                throw new E("该视频已关联该分类");
            }
            ClassifyVideo classifyVideo = new ClassifyVideo();
            classifyVideo.setClassifyId(req.getClassifyId());
            classifyVideo.setVideoId(req.getId());
            this.classifyVideoService.save(classifyVideo);
        }
        return B.okBuild();
    }

    @Override
    public B<Void> delete(BaseDeleteEntity req) {
        this.removeByIds(req.getIds());
        return B.okBuild();
    }
}
